package com.niiwoo.civet.account.service.local.trade;

import com.alibaba.fastjson.JSON;
import com.niiwoo.civet.account.dao.entity.WithholdOrder;
import com.niiwoo.civet.account.dao.entity.WithholdOrderExample;
import com.niiwoo.civet.account.dao.mapper.WithholdOrderMapperExt;
import com.niiwoo.civet.account.enums.WithholdOrderStatusEnum;
import com.niiwoo.tripod.lanmao.component.LanMaoSequence;
import com.niiwoo.tripod.service.component.SnowflakeIdWorker;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * Created by xiaoyingping on 2018/4/25.
 */

@Service
@Slf4j
public class WithholdOrderService {

    @Autowired
    private WithholdOrderMapperExt withholdOrderMapperExt;

    @Autowired
    private SnowflakeIdWorker snowflakeIdWorker;
    @Autowired
    private LanMaoSequence lanMaoSequence;


    /**
     * 新增代扣订单
     * @param withholdOrder
     */
    @Transactional
    public void addWithholdOrder(WithholdOrder withholdOrder){
        withholdOrder.setId(snowflakeIdWorker.nextId());
        withholdOrder.setRequestNo(lanMaoSequence.getRequestNo());
        withholdOrder.setStatus(WithholdOrderStatusEnum.HANDLING.getValue());
        Date nowDate = new Date();
        withholdOrder.setCreateTime(nowDate);
        withholdOrder.setUpdateTime(nowDate);

        log.info("新增代扣订单:{}", JSON.toJSONString(withholdOrder));
        withholdOrderMapperExt.insertSelective(withholdOrder);
    }

    /**
     * 修改代扣状态为成功
     * @param id
     */
    @Transactional
    public void updateWithholdOrderSuccess(Long id){
        log.info("将代扣订单的状态修改为成功");
        WithholdOrder record = new WithholdOrder();
        record.setId(id);
        record.setStatus(WithholdOrderStatusEnum.SUCCESS.getValue());
        Date nowDate = new Date();
        record.setSuccessTime(nowDate);
        record.setUpdateTime(nowDate);
        withholdOrderMapperExt.updateByPrimaryKeySelective(record);
    }

    /**
     * 修改代扣状态为失败
     * @param id
     * @param errorCode
     * @param errorMsg
     */
    @Transactional
    public void updateWithholdOrderFail(Long id, String errorCode, String errorMsg){
        log.info("将代扣订单的状态修改为失败");
        WithholdOrder record = new WithholdOrder();
        record.setId(id);
        record.setStatus(WithholdOrderStatusEnum.FAIL.getValue());
        record.setErrorCode(errorCode);
        record.setErrorMessage(errorMsg);
        record.setUpdateTime(new Date());
        withholdOrderMapperExt.updateByPrimaryKeySelective(record);
    }

    /**
     * 查询ForUpdate
     * @param id
     * @return
     */
    public WithholdOrder selectByPrimaryKeyForUpdate(Long id){
        return withholdOrderMapperExt.selectByPrimaryKeyForUpdate(id);
    }


    /**
     * 通过requestNo查询代扣订单
     * @param requestNo
     * @return
     */
    public WithholdOrder loadWithholdOrderByRequestNo(String requestNo){
        Assert.hasLength(requestNo,"requestNo不能为空");
        WithholdOrder withholdOrder = withholdOrderMapperExt.selectOrderByRequestNo(requestNo);
        if(Objects.isNull(withholdOrder)){
            throw new RuntimeException("WithholdOrder不存在, requestNo:" + requestNo);
        }

        return withholdOrder;
    }

    /**
     * 通过orderId查询代扣订单
     * @param orderId
     * @return
     */
    public WithholdOrder getWithholdOrderByOrderId(Long orderId){
        Assert.notNull(orderId,"orderId不能为空");
        WithholdOrderExample example = new WithholdOrderExample();
        WithholdOrderExample.Criteria criteria = example.createCriteria();
        criteria.andOrderIdEqualTo(orderId);
        List<WithholdOrder> list = withholdOrderMapperExt.selectByExample(example);
        if(CollectionUtils.isEmpty(list)){
            return null;
        }

        return list.get(0);
    }

}
